﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Common.BaseLibrary.Authorization;
using DepartmentWork.Migrator.Data;
using DepartmentWork.Model;
using DepartmentWork.Platform.Controllers;

namespace DepartmentWork.Platform.Areas.SystemSettingArea.Controllers
{
    public class MenuController : BaseController
    {
        //
        // GET: /SystemSettingArea/Menu/
        public ActionResult Index(string flag = "", string flagInfo = "")
        {
            ViewBag.Flag = flag;
            ViewBag.FlagInfo = flagInfo;

            #region 校验身份

            bool isAdmin = false;
            try
            {
                string[] pathArr = Request.Path.Split('/');
                Guid userGuid = new Guid(CurrentUser.UserID);
                USER_INFO user = userRepository.GetModel(r => r.USER_ID == userGuid).FirstOrDefault();
                USER_TEAMSAPCE userTeam = userTeamsapceRepository.GetModel(r => r.USER_ID == user.USER_ID).FirstOrDefault();
                ROLE_INFO role = roleRepository.GetModel(r => r.ROLE_ID == userTeam.ROLE_ID).FirstOrDefault();
                List<MENU_ROLE> menuRole = menuRoleRepository.GetModel(r => r.ROLE_ID == role.ROLE_ID).ToList();
                for (int i = 0; i < menuRole.Count(); i++)
                {
                    int menuId = menuRole[i].MENU_ID;
                    MENU menu = menuRepository.GetModel(r => r.MENU_ID == menuId).FirstOrDefault();
                    if (menu.AREA_NAME == pathArr[2] && menu.CONTROLLER_NAME == pathArr[3])
                    {
                        isAdmin = true;
                        break;
                    }
                }
            }
            catch (Exception)
            {
                return this.RedirectToAction("Index", "Home", new { area = "", info = "当前用户不具有管理员权限，无法操作菜单！" });
            }

            if (isAdmin==false)
            {
                return this.RedirectToAction("Index", "Home", new {area = "",info="当前用户不具有管理员权限，无法操作菜单！"});
            }


            #endregion


            List<MenuViewModel> menuModel = new List<MenuViewModel>();
            menuModel = new List<MenuViewModel>();
            List<MENU> menuList = menuRepository.GetModel().ToList();
            menuModel.AddRange((from item in menuList
                                select new MenuViewModel()
                                {
                                    ACTION_NAME = item.ACTION_NAME,
                                    AREA_NAME = item.AREA_NAME,
                                    BOOT_MENU_ID = item.BOOT_MENU_ID,
                                    CONTROLLER_NAME = item.CONTROLLER_NAME,
                                    MENU_CONTEXT = item.MENU_CONTEXT,
                                    MENU_ID = item.MENU_ID,
                                    MENU_NAME = item.MENU_NAME,
                                    MENU_STATUS = item.MENU_STATUS,
                                    MENU_CLASS = item.MENU_CLASS
                                }).ToList());

            for (int i = 0; i < menuModel.Count(); i++)
            {
                int id = menuModel[i].MENU_ID;
                menuModel[i].CHILDEN_COUNT = menuModel.FindAll(r => r.BOOT_MENU_ID == id).Count();
            }
            return View(menuModel);
        }

        [HttpPost]
        public ActionResult CreateMenu(FormCollection collection)
        {
            try
            {
                //int id = menuRepository.GetModel().LastOrDefault().MENU_ID;
                MENU model = new MENU();
                //model.MENU_ID = id + 1;
                model.MENU_NAME = collection["tb_CreateMenuTitle"];
                model.AREA_NAME = collection["tb_CreateMenuAreaName"];
                model.CONTROLLER_NAME = collection["tb_CreateMenuControllerName"];
                model.ACTION_NAME = collection["tb_CreateMenuActionName"];
                model.BOOT_MENU_ID = Convert.ToInt32(collection["select_CreateMenuBoot"]);
                model.MENU_CLASS = collection["select_CreateMenuIcon"];
                model.MENU_CONTEXT = collection["tb_CreateMenuContext"]??"没有描述";
                model.MENU_STATUS = 0;
                menuRepository.Insert(model);
                return RedirectToAction("Index", "Menu", new { area = "SystemSettingArea", flag = "CreateTrue" });
            }
            catch (Exception ex)
            {
                return RedirectToAction("Index", "Menu", new { area = "SystemSettingArea", flag = "CreateFalse",info=ex.Message });
            }
        }

        public JsonResult GetMenuInfo(int menuId)
        {
            MENU menu = menuRepository.GetModel(r => r.MENU_ID == menuId).FirstOrDefault();
            MenuViewModel model = new MenuViewModel();
            model.AREA_NAME = menu.AREA_NAME;
            model.ACTION_NAME = menu.ACTION_NAME;
            model.BOOT_MENU_ID = menu.BOOT_MENU_ID;
            model.CONTROLLER_NAME = menu.CONTROLLER_NAME;
            model.MENU_CLASS = menu.MENU_CLASS;
            model.MENU_CONTEXT = menu.MENU_CONTEXT;
            model.MENU_ID = menu.MENU_ID;
            model.MENU_NAME = menu.MENU_NAME;
            model.MENU_STATUS = menu.MENU_STATUS;
            return this.Json(model);
        }

        public ActionResult EditMenu(FormCollection collection)
        {
            try
            {
                int menuId = Convert.ToInt32(collection["hid_EditMenuId"]);
                MENU model = menuRepository.GetModel(r => r.MENU_ID == menuId).FirstOrDefault();
                model.ACTION_NAME = collection["tb_EditMenuActionName"].Trim() == "" ? model.ACTION_NAME : collection["tb_EditMenuActionName"];
                model.CONTROLLER_NAME = collection["tb_EditMenuControllerName"].Trim() == "" ? model.CONTROLLER_NAME : collection["tb_EditMenuControllerName"];
                model.AREA_NAME = collection["tb_EditMenuAreaName"].Trim() == "" ? model.AREA_NAME : collection["tb_EditMenuAreaName"];
                model.MENU_CONTEXT = collection["tb_EditMenuContext"].Trim() == "" ? model.MENU_CONTEXT : collection["tb_EditMenuContext"];
                model.MENU_NAME = collection["tb_EditMenuTitle"].Trim() == "" ? model.MENU_NAME : collection["tb_EditMenuTitle"];
                model.MENU_CLASS = collection["select_EditMenuIcon"];
                model.BOOT_MENU_ID = Convert.ToInt32(collection["select_EditMenuBoot"]);
                if (collection["tb_EditMenuStatue"] == "0")
                {
                    model.MENU_STATUS = 0;
                }
                else
                {
                    model.MENU_STATUS = 1;
                }
                menuRepository.Update(model);
                return RedirectToAction("Index", "Menu", new { area = "SystemSettingArea", flag = "EditTrue" });
            }
            catch (Exception ex)
            {
                return RedirectToAction("Index", "Menu", new { area = "SystemSettingArea", flag = "EditFalse", info = ex.Message });
            }
        }

        public ActionResult DelMenu(int menuId)
        {
            try
            {
                List<MENU_ROLE> menuRole = menuRoleRepository.GetModel(r => r.MENU_ID == menuId).ToList();
                for (int i = 0; i < menuRole.Count(); i++)
                {
                    menuRoleRepository.Delete(menuRole[i]);
                }
                MENU menu = menuRepository.GetModel(r => r.MENU_ID == menuId).FirstOrDefault();
                menuRepository.Delete(menu);
                return RedirectToAction("Index", "Menu", new { area = "SystemSettingArea", flag = "DelTrue" });
            }
            catch (Exception ex)
            {
                return RedirectToAction("Index", "Menu", new { area = "SystemSettingArea", flag = "EditFalse", info = ex.Message });
            }

        }

    }
}
